package center_rpc

import (
	"common/pb"
	"common/uuid"
	"fmt"
)

type NodeInfo struct {
	NodeId uuid.UUID
	DevId  string
	Ip     string
	Ipv6   string
	Port   uint16
	//Fwd    bool
	//DialWithP2P bool
	PieceNum int
}

func (n *NodeInfo) String() string {
	return fmt.Sprintf("num %d nodeid %s devid %s port %d", n.PieceNum, n.NodeId.String(), n.DevId, n.Port)
}

func ConverSegmentToNodeInfo(segmentDowload *pb.SegmentDownload) []NodeInfo {
	//转换数据
	nodeInfos := make([]NodeInfo, 0)
	for _, p := range segmentDowload.Pieces {
		//fmt.Printf("nodeinfo %d %+v\n", p.Index, p.NodeInfo)
		_nodeId, _ := uuid.FromBytes(p.NodeInfo.NodeId)
		nodeInfos = append(nodeInfos, NodeInfo{
			DevId: p.NodeInfo.StorageNodeAddress.DeviceId,
			Ip:    p.NodeInfo.StorageNodeAddress.Ip,
			Ipv6:  p.NodeInfo.StorageNodeAddress.Ipv6,
			Port:  uint16(p.NodeInfo.StorageNodeAddress.Port),
			//Fwd:      p.NodeInfo.PeerFwd == 1,
			NodeId:   _nodeId,
			PieceNum: int(p.Index),
		})
	}
	//for i := range nodeInfos {
	//	fmt.Println(nodeInfos[i].String())
	//}
	return nodeInfos
}
